package com.mdlib.helper.module.account.fragments;

import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.core.widget.CountdownButton;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.interfaces.ITextChangedListener;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.utils.MDAppUtils;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by librabin on 16/11/24.
 */

public class ForgetFirstFragment extends BaseTitleFragment {


    @Bind(R.id.btn_featch_code)
    CountdownButton mFeatchCodeButton;

    @Bind(R.id.et_forget_phone)
    EditText mPhoneNumField;

    @Bind(R.id.et_forget_code)
    EditText mPhoneCodeField;

    @Bind(R.id.tv_forget_next)
    TextView mTvForgetNext;

    private String mCodeType = "forget";


    public static ForgetFirstFragment newInstance() {
        return new ForgetFirstFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_forget_firstep;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.forget_tv_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });

        mPhoneNumField.addTextChangedListener(new ITextChangedListener(){
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11 && !mFeatchCodeButton.isRunning()) {
                    mFeatchCodeButton.setEnabled(true);
                } else {
                    mFeatchCodeButton.setEnabled(false);
                }
            }
        });
    }


    @OnClick({R.id.btn_featch_code, R.id.tv_forget_next})
    public void onClick(View view) {
        String phoneNum = mPhoneNumField.getText().toString();
        String phoneCode = mPhoneCodeField.getText().toString();

        switch (view.getId()) {
            case R.id.btn_featch_code:
                if (!MDAppUtils.checkPhoneNum(phoneNum)) {
                    return;
                }
                featchCode(phoneNum, mCodeType);
                break;
            case R.id.tv_forget_next:
                if (!MDAppUtils.checkPhoneNum(phoneNum)) {
                    return;
                }

                if (!MDAppUtils.checkPhoneCode(phoneCode)) {
                    return;
                }
                verifyPhoneCode(phoneNum, phoneCode, mCodeType);
                break;
        }
    }



    private void featchCode(String phoneNum, String codeType) {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .featchPhoneCode(phoneNum, codeType)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {

                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));

    }


    private void verifyPhoneCode(final String phoneNum, String phoneCode, String codeType) {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .verifyPhoneCode(phoneNum, phoneCode, codeType)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        addFragment(ForgetSecondFragment.newInstance(phoneNum));
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));

    }




}
